package com.liu.project.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.project.system.domain.po.MenuPo;
import com.liu.project.system.mapper.MenuMapper;
import com.liu.project.system.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lxl
 * @date 2023/1/30 13:25
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPo> implements MenuService {


    @Override
    public List<Tree<Integer>> nodeList() {
        //查询所有菜单信息
        val menuList = this.list();
        // 2.配置
        TreeNodeConfig config = new TreeNodeConfig();
        config.setWeightKey("priority");//排序字段

        // 3.转树，Tree<>里面泛型为id的类型
        List<Tree<Integer>> build = TreeUtil.build(menuList, 0, config, (object, tree) -> {
            // 也可以使用 tree.setId(object.getId());等一些默认值
            tree.putExtra("id", object.getMenuId());
            tree.putExtra("parentId", object.getParentId());
            tree.putExtra("icon", object.getIcon());
            tree.putExtra("name", object.getMenuName());
            tree.putExtra("url", object.getUrl());
            tree.putExtra("type", object.getRequestType());
        });
        return build;
    }
}
